'''
Created on 2011-3-5

@author: roamer
'''
from db.google.dbmodel import *
from parser import feedparser
import datetime
import jsonpickle

class dbutil(object):
    '''
    An util class for google datastore api
    '''
    def __init__(self, out):
        self.out = out;

    def add_one_feed(self, url):
        d = feedparser.parse(url)
        if d.status == 200 and d.feed:
            if not d.feed.has_key("title") or \
               not d.feed.has_key("link"):
                return False, "Feed not contain title and link."
                           
            feed = rss_feed(title = d.feed.title, url = d.feed.link, \
                            addTime = datetime.datetime.now(), \
                            lastUpdate = datetime.datetime.now())
            if d.feed.has_key("info"):
                feed.description = d.feed.info
                
            self.out(jsonpickle.encode(feed))
        